package com.xing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.Utils.Result;
import com.xing.config.JSONUtils;
import com.xing.dao.LiveReportDao;
import com.xing.dto.LiveReportDTO;
import com.xing.dto.SysNotificationDTO;
import com.xing.dto.SysUserDTO;
import com.xing.entity.LiveReportEntity;
import com.xing.feign.NotificationFeign;
import com.xing.feign.UserFeign;
import com.xing.service.LiveReportService;
import com.xing.utils.PageData;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LiveReportServiceImpl extends ServiceImpl<LiveReportDao, LiveReportEntity> implements LiveReportService {

    @Autowired
    private LiveReportDao reportDao;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private NotificationFeign notificationFeign;


    @Override
    public List<LiveReportEntity> listAllReport() {
        // 添加查询条件
        LambdaQueryWrapper<LiveReportEntity> lambdaQueryWrapper = Wrappers.lambdaQuery(LiveReportEntity.class)
                .eq(LiveReportEntity::getResult, 0)
                .eq(LiveReportEntity::getDelFlag, 0);
        // 进行查询
        return reportDao.selectList(lambdaQueryWrapper);
    }


    @Override
    public PageData<LiveReportDTO> pageReport(Integer pageNum, Integer pageSize) {
        // 添加查询条件
        LambdaQueryWrapper<LiveReportEntity> lambdaQueryWrapper = Wrappers.lambdaQuery(LiveReportEntity.class)
                .eq(LiveReportEntity::getResult, 0)
                .eq(LiveReportEntity::getDelFlag, 0);
        Page<LiveReportEntity> page = new Page<>(pageNum, pageSize);
        // 进行分页查询
        Page<LiveReportEntity> liveReportEntityPage = reportDao.selectPage(page, lambdaQueryWrapper);
        List<LiveReportDTO> dtos = new ArrayList<>();
        for (LiveReportEntity record : liveReportEntityPage.getRecords()) {
            LiveReportDTO liveReportDTO = JSONUtils.objParse(record, LiveReportDTO.class);
            Result userById = userFeign.getUserById(record.getUserId().toString());
            assert liveReportDTO != null;
            liveReportDTO.setUserDTO(JSONUtils.objParse(userById.getData(), SysUserDTO.class));
            dtos.add(liveReportDTO);
        }
        return new PageData<>(dtos, liveReportEntityPage.getTotal());
    }


    @Override
    public Result handleReport(LiveReportDTO dto) {
        UpdateWrapper<LiveReportEntity> wrapper = new UpdateWrapper<>();
        wrapper.set(!StringUtils.isEmpty(dto.getAdminId())
                        , "admin_id"
                        , dto.getAdminId())
                .set(!StringUtils.isEmpty(dto.getResult())
                        , "result"
                        , dto.getResult())
                .eq("id", dto.getId());
        boolean update = update(wrapper);
        if (update) {
            SysNotificationDTO sysNotificationDTO = new SysNotificationDTO();
            sysNotificationDTO.setCreateDate(new Date());
            sysNotificationDTO.setTitle("举报处理");
            String result = dto.getResult() == 1 ? "举报成功" : "经审核无违规操作";
            sysNotificationDTO.setContent("你的举报结果为" + result + ",如果有其他问题请咨询客服处理。");
            sysNotificationDTO.setReceivedId(dto.getUserId());
            notificationFeign.sendNotification(sysNotificationDTO);
            return new Result().ok();
        }
        return new Result().err(568, "缺少数据或数据错误");
    }

    @Override
    public Result addReport(LiveReportDTO dto) {
        boolean save = save(JSONUtils.objParse(dto, LiveReportEntity.class));
        if (save) {
            return new Result().ok();
        }
        return new Result().err(501);
    }
}
